/*
 * TableModel.java
 *
 * This class defines the table model for tables
 * used  in the GUI.
 *
 * Created on Apr 30, 2011, 8:59:18 PM
 */

package gui;
import javax.swing.table.*;

/**
 * Class to define the "edit" tables
 * @author Sarah Wegner
 */

    class TableModel extends DefaultTableModel {
        private String[] colNames;
        private Object[][] rowData;
        TableModel(Object[][] rowData, String[] colNames){
            super(rowData, colNames);
            this.colNames = colNames;
            this.rowData = rowData;
        }

        @Override
        public Class getColumnClass(int columnIndex) {
            return getValueAt(0, columnIndex).getClass();
        }

        @Override
        public String getColumnName(int col) {
            return colNames[col];
        }

        public boolean isCellSelected(int rowIndex, int columnIndex){
            return true;
        }

        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            int colCount = getColumnCount();
            String colName = getColumnName(colCount-1);

            //Deleted column is not editable
            if(getColumnName(columnIndex).equals("Deleted")){
                return false;
            }

            //if record is deleted, the row is not editable, but Family Tab cannot be deleted
            if(colCount != 7){//colCount==7 means it is the family tab
                if((columnIndex < colCount) && (columnIndex >=0) && ((Boolean)getValueAt(rowIndex,colCount-1)) && (colName.equals("Deleted"))){
                    return false;
                }//if
            }//if
            return true;
        }//isCellEditable

    }//TableModel